package com.mm.dss.demo;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.Slide;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mm.dss.demo.Interface.OkGoUtilsInterFace;
import com.mm.dss.demo.base.BaseActivity;
import com.mm.dss.demo.tools.Constances;
import com.mm.dss.demo.tools.OkgoUtils;
import com.mm.dss.demo.tools.SharedPreferencedUtils;
import com.mm.dss.demo.tools.WebServiceUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.serialization.SoapObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.OnClick;

public class SLoginActivity extends BaseActivity {
    private EditText name;//用户名
    private EditText password;//密码
    private  TextView btn_login;//登陆按钮
    public static   Activity activity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slogin);
        activity=SLoginActivity.this;
        initview();
        initdata();
    }
    private void initview() {
        btn_login= (TextView) findViewById(R.id.btn_login);
        name= (EditText) findViewById(R.id.name);
        password= (EditText) findViewById(R.id.password);
       if (SharedPreferencedUtils.getString(SLoginActivity.this,"username")!=null){
            Log.i("执行了",SharedPreferencedUtils.getString(SLoginActivity.this,"username"));
            if (!SharedPreferencedUtils.getString(SLoginActivity.this,"username").equals("")){
                startActivity(new Intent(SLoginActivity.this, ZhuMainActivity.class));
                finish();
            }
        }
    }
    private void initdata() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Map<String,String> map=new HashMap<>();
                map.put("username",name.getText().toString());
                map.put("password",password.getText().toString());
                WebServiceUtils.callWebService(Constances.webhosturl, "N01_login", map, new WebServiceUtils.WebServiceCallBack() {
                    @Override
                    public void callBack(SoapObject result) {
                        Log.i("登陆返回信息",result.getProperty(0).toString());
                        try {
                            JSONArray array=new JSONArray(result.getProperty(0).toString());
                            JSONObject object=array.optJSONObject(0);
                            String username=object.optString("username");
                            String rusername=object.getString("realname");
                            String Level=object.getString("Role");   //1：管理员;0普通用户
                            Log.i("解析的",username+"/"+rusername);
                            SharedPreferencedUtils.setString(SLoginActivity.this,"username",username);
                            SharedPreferencedUtils.setString(SLoginActivity.this,"realname",rusername);
                            SharedPreferencedUtils.setString(SLoginActivity.this,"Level",Level);
                            startActivity(new Intent(SLoginActivity.this,ZhuMainActivity.class));
                            finish();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }




}
